﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Cua_Hang.DTO;

namespace Cua_Hang.Admin
{
    public partial class QuanLy_ChiTiet_CuaHang : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //this.ListChiTiet_CuaHangAll_Admin_GUI();
            //this.ShowCuaHang_Gui();
            if (Session["Login"] != null)
            {
                this.ListChiTiet_CuaHangAll_Theo_id_Admin_GUI();
                this.ShowCuaHang_Theo_id_Gui();
                this.Bt_capnhatsanpham.Visible = false;
                //this.Tb_TenSanPham.Text = Convert.ToInt32(DDL_Tencuahang.SelectedValue).ToString();
            }
            else
            {
                Response.Redirect("http://localhost:1482/DangNhap.aspx");
            }
        }
        //========================================================
        //--------Hien tat ca cac san pham cua cua hang--------------------
        void ListChiTiet_CuaHangAll_Admin_GUI()
        {

            GV_ChiTietCuaHang.DataSource=Cua_Hang.BUS.ChiTiet_CuaHang_BUS.ListChiTiet_CuaHangAll_Admin_BUS();
            GV_ChiTietCuaHang.DataBind();
        }
        //--------ket thuc Hien tat ca cac san pham cua cua hang------------
        //========================================================
        //========================================================
        //--------Hien tat ca cac san pham cua cua hang theo id--------------------
        void ListChiTiet_CuaHangAll_Theo_id_Admin_GUI()
        {
            //cho nay chua lam dang nhap
            int ma = Convert.ToInt32(Session["Login"].ToString()); //cho nay chua lam dang nhap
            GV_ChiTietCuaHang.DataSource = Cua_Hang.BUS.ChiTiet_CuaHang_BUS.ListChiTiet_CuaHangAll_BUS(ma);//nho dang nhap
            GV_ChiTietCuaHang.DataBind();
        }
        //--------ket thuc Hien tat ca cac san pham cua cua hang theo id------------
        //========================================================
        //show cua hang-----------
        void ShowCuaHang_Gui()
        {
            Cua_Hang.DTO.Cua_Hang_DTO chang = new DTO.Cua_Hang_DTO();
            DDL_Tencuahang.DataSource = Cua_Hang.BUS.Cua_Hang_BUS.ListCuaHangAll_BUS();
            DDL_Tencuahang.DataTextField = "Ten_cuahang";
            DDL_Tencuahang.DataValueField = "ID_cuahang";
            DDL_Tencuahang.DataBind();
            //chang.ID_cuahang = int.Parse(Tbxoa_idcuahang.Text);
            //Cua_Hang.BUS.Cua_Hang_BUS.XoaCuaHang_BUS(chang);
        }
        //ket thuc show cua hang-----------
        //========================================================
        //========================================================
        //show cua hang theo id-----------
        void ShowCuaHang_Theo_id_Gui()
        {
            Cua_Hang.DTO.Cua_Hang_DTO chang = new DTO.Cua_Hang_DTO();
            //cho nay chua lam dang nhap
            DDL_Tencuahang.DataSource = Cua_Hang.BUS.Cua_Hang_BUS.ListCuaHangTheo_id_BUS(Convert.ToInt32(Session["Login"].ToString()));//nho cho nay
            DDL_Tencuahang.DataTextField = "Ten_cuahang";
            DDL_Tencuahang.DataValueField = "ID_cuahang";
            DDL_Tencuahang.DataBind();
            //chang.ID_cuahang = int.Parse(Tbxoa_idcuahang.Text);
            //Cua_Hang.BUS.Cua_Hang_BUS.XoaCuaHang_BUS(chang);
        }
        //ket thuc show cua hang theo id----------
        protected void GV_ChiTietCuaHang_SelectedIndexChanged(object sender, EventArgs e)
        {
            int ID_ChiTiet_CuaHang = Convert.ToInt32(GV_ChiTietCuaHang.SelectedRow.Cells[0].Text);
            ChiTiet_CuaHang_DTO dmn = new ChiTiet_CuaHang_DTO();
            dmn = ShowChiTiet_CuaHangTheoID(ID_ChiTiet_CuaHang);
            Tb_TenSanPham.Text = dmn.Ten_ChiTiet_CuaHang.ToString();
            Id_tensanpham.Text = ID_ChiTiet_CuaHang.ToString();
            this.Bt_themsanpham.Visible = false;
            this.Bt_capnhatsanpham.Visible = true;
        }
        //============= chuyen List thanh DataTable===========================
        //
        public DataTable ConvertListCuaHangDTOToDataTable(List<ChiTiet_CuaHang_DTO> lst)
        {

            DataTable dt = new DataTable();
            dt.Columns.Add("ID_ChiTiet_CuaHang");
            dt.Columns.Add("ID_cuahang");
            dt.Columns.Add("Ten_ChiTiet_CuaHang");
            for (int i = 0; i < lst.Count; i++)
            {
                dt.Rows.Add(
                        lst[i].ID_ChiTiet_CuaHang,
                        lst[i].ID_cuahang,
                        lst[i].Ten_ChiTiet_CuaHang

                    );
            }
            return dt;
        }
        //============= ket thuc chuyen List thanh DataTable=======================
        //========================================================
        //==============ham hien vao textbox ten cua hang theo id =================
        public ChiTiet_CuaHang_DTO ShowChiTiet_CuaHangTheoID(int ID_ChiTiet_CuaHang)
        {
            ChiTiet_CuaHang_DTO dm = new ChiTiet_CuaHang_DTO();
            List<ChiTiet_CuaHang_DTO> lst = BUS.ChiTiet_CuaHang_BUS.ListChiTiet_CuaHangTheoID_BUS(ID_ChiTiet_CuaHang);
            DataTable dt = new DataTable();
            dt = ConvertListCuaHangDTOToDataTable(lst);
            foreach (DataRow dr in dt.Rows)
            {
                //dm.ID_cuahang = (int)dr["ID_cuahang"];
                //dm.ID_cuahang = (int)dr["ID_cuahang"];
                dm.Ten_ChiTiet_CuaHang = dr["Ten_ChiTiet_CuaHang"].ToString();

            }
            return dm;
        }
        //======================================================================
        //===========day la ham cap nhat========================

        //-----Lay Thong tin tu chi tiet cua hang tren textbox 
        private void LayThongTinChiTietCuaHang(ref ChiTiet_CuaHang_DTO dm)
        {

            dm.ID_cuahang = Convert.ToInt32(Id_tensanpham.Text);
            dm.Ten_ChiTiet_CuaHang = Tb_TenSanPham.Text;

        }
        //-----ket thuc Lay Thong tin tu chi tiet cua hang tren textbox 

        //----ham cap nhat chi tiet cua hang
        private void CapNhatChiTietCuaHang()
        {
            ChiTiet_CuaHang_DTO dm = new ChiTiet_CuaHang_DTO();
            //LayThongTinChiTietCuaHang(ref dm);
            dm.ID_ChiTiet_CuaHang = Convert.ToInt32(Id_tensanpham.Text);
            dm.Ten_ChiTiet_CuaHang = Tb_TenSanPham.Text;
            Cua_Hang.BUS.ChiTiet_CuaHang_BUS.SuaChiTietCuaHang_BUS(dm);

        }
        //----ket thuc ham cap nhat chi tiet cua hang
        protected void Bt_capnhatsanpham_Click(object sender, EventArgs e)
        {
            if(KiemTra()==true)
            {
                this.CapNhatChiTietCuaHang();
                this.Bt_themsanpham.Visible = true;
                Tb_TenSanPham.Text = "";
                this.ListChiTiet_CuaHangAll_Theo_id_Admin_GUI();
            }
        }
        //========================================================================
        //----------them chi tiet cua hang------------------------------
        void ThemChiTietCuaHang_GUI()
        {
            ChiTiet_CuaHang_DTO ct = new ChiTiet_CuaHang_DTO();
            ct.ID_cuahang=Convert.ToInt32(DDL_Tencuahang.SelectedValue);
            ct.Ten_ChiTiet_CuaHang = Tb_TenSanPham.Text;
            Cua_Hang.BUS.ChiTiet_CuaHang_BUS.ThemChiTietCuaHang_BUS(ct);
        }
        
        protected void Bt_themsanpham_Click(object sender, EventArgs e)
        {
            Cua_Hang.DTO.ChiTiet_CuaHang_DTO chang = new DTO.ChiTiet_CuaHang_DTO();
            chang.Ten_ChiTiet_CuaHang = Tb_TenSanPham.Text;
            if(KiemTra() == true )
            {
                if ( Cua_Hang.BUS.ChiTiet_CuaHang_BUS.KiemTraTrung_ChiTiet_CuaHang_BUS(chang) == false)
                {
                    this.ThemChiTietCuaHang_GUI();
                    this.ListChiTiet_CuaHangAll_Theo_id_Admin_GUI();

                    Tb_TenSanPham.Text = "";
                }
                else
                {
                    Response.Write(@"<script language = 'javascript'>alert ('Tên sản phẩm đã tồn tại !'); </script>");
                }
            }
        }

        protected void GV_ChiTietCuaHang_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            Int32 index = Int32.Parse(e.CommandArgument.ToString());
            if (e.CommandName == "Xoa")
            {
                //String Code = GV_ChiTietCuaHang.DataKeys[index].Value.ToString();
                Cua_Hang.DTO.ChiTiet_CuaHang_DTO chang = new DTO.ChiTiet_CuaHang_DTO();
                chang.ID_ChiTiet_CuaHang = Convert.ToInt32(e.CommandArgument);
                Cua_Hang.BUS.ChiTiet_CuaHang_BUS.XoaChiTietCuaHang_BUS(chang);
                this.ListChiTiet_CuaHangAll_Theo_id_Admin_GUI();
            }
        }

        protected void GV_ChiTietCuaHang_PageIndexChanged(object sender, EventArgs e)
        {

        }

        protected void GV_ChiTietCuaHang_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GV_ChiTietCuaHang.PageIndex = e.NewPageIndex;
            this.ListChiTiet_CuaHangAll_Theo_id_Admin_GUI();
        }
        //==============ham hien vao textbox ten cua hang theo id =================
        //====================ham kiem tra================================
        private bool KiemTra()
        {
            if (this.Tb_TenSanPham.Text.Trim() == "")
            {
                Response.Write(@"<script language = 'javascript'>alert ('Bạn chưa nhập tên sản phẩm!'); </script>");

                return false;
            }
            return true;

        }

    }
}